package com.sjk.flink.schema;

import com.fasterxml.jackson.databind.JsonNode;
import networknt.schema.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

public class NotNullValidator extends BaseJsonValidator {
    private static final Logger logger = LoggerFactory.getLogger(NotNullValidator.class);
    private static final String NOTNULL = "NotNull";

    public NotNullValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, ValidatorTypeCode type) {
        super(schemaPath, schemaNode, parentSchema, type, validationContext);

        this.validationContext = validationContext;
        parseErrorCode(getValidatorType().getErrorCodeKey());
    }

    @Override
    public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
        debug(logger, node, rootNode, at);
        Set<ValidationMessage> errors = new LinkedHashSet<>();
        JsonType nodeType = TypeFactory.getValueNodeType(node, this.validationContext.getConfig());
        System.out.println("nodeType >>>>>>>>>>>>>>>>  "+nodeType);
        if (nodeType != JsonType.STRING) {
            return errors;
        }
        String val = node.textValue();
        System.out.println("val >>>>>>>>>>>>>>>>  "+val);
        if (val==null||val.isEmpty()) {
            errors.add(buildValidationMessage(at, val, NOTNULL));
        }
        return Collections.unmodifiableSet(errors);
    }
}
